#include<vector>
#include<algorithm>
using namespace std;

class Solution {
public:
    int maxTurbulenceSize(vector<int>& A) {
        if (A.size() < 2) return A.size();
        int left = 0, right = 0;
        int maxLen = 1;
        while (left+maxLen < A.size()-1) {
            right = left + 1;
            if (A[left] != A[right++]) {
                bool flag = A[left] < A[right-1];
                while (right < A.size()) {
                    if ((flag && A[right - 1] > A[right])||
                        (!flag && A[right - 1] < A[right])) {
                        flag = !flag;
                        ++right;
                    }
                    else
                        break;
                }
                maxLen = max(maxLen, right - left);
            }
            left = right - 1;
        }
        return maxLen;

    }
};